﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using cpe.cpein.DATA;

namespace cpe.cpein.BUSINESS
{
    public static class BURecommendation
    {
        /// <summary>
        /// Get All the Recommendation of the database.
        /// </summary>
        /// <param name="searchInDeleted"></param>
        /// <returns></returns>
        public static List<Recommendation> GetAllRecommendation(bool searchInDeleted)
        {
            List<Recommendation> Recommendations = new List<Recommendation>();
            using (cpeinEntities db = new cpeinEntities())
            {
                var results = from Recommendation in db.Recommendation
                              where Recommendation.rec_isdeleted == searchInDeleted
                              select Recommendation;
                foreach (Recommendation tmp in results)
                    Recommendations.Add(tmp);
            }

            return Recommendations;
        }

        /// <summary>
        /// Get the Recommendation by id.
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static Recommendation GetRecommendationByID(int id)
        {
            if (id <= 0)
                return null;
            else
            {
                using (cpeinEntities db = new cpeinEntities())
                {
                    var rec = from Recommendation in db.Recommendation
                              where Recommendation.rec_id == id
                              select Recommendation;
                    foreach (Recommendation tmp in rec)
                        return tmp;
                    return null;
                }
            }
        }

        /// <summary>
        /// rec a Recommendation into the database.
        /// </summary>
        /// <param name="rec"></param>
        /// <returns></returns>
        public static Recommendation Recommendationrea(Recommendation rec)
        {
            if (rec != null)
            {
                using (cpeinEntities db = new cpeinEntities())
                {
                    Recommendation nrec = db.Recommendation.Add(rec);
                    db.SaveChanges();
                    return nrec;
                }
            }
            else
                return null;
        }

        /// <summary>
        /// Remove a specific Recommendation of the database.
        /// </summary>
        /// <param name="rec"></param>
        /// <returns></returns>
        public static bool RemoveRecommendation(Recommendation rec)
        {
            if (rec != null)
            {
                rec.rec_isdeleted = true;
                if (UpdateRecommendation(rec) != null)
                {
                    using (cpeinEntities db = new cpeinEntities())
                    {
                        db.SaveChanges();
                        return true;
                    }
                }
            }

            return false;
        }

        /// <summary>
        /// Update the Recommendation into the database.
        /// </summary>
        /// <param name="rec"></param>
        /// <returns></returns>
        public static Recommendation UpdateRecommendation(Recommendation rec)
        {
            if (rec != null)
            {
                using (cpeinEntities db = new cpeinEntities())
                {
                    var current = from Recommendation in db.Recommendation
                                  where Recommendation.rec_id == rec.rec_id
                                  select Recommendation;

                    foreach (var tmp in current)
                    {
                        tmp.usr_writer_id = rec.usr_writer_id;
                        tmp.usr_subject_id = rec.usr_subject_id;
                        tmp.rec_date = rec.rec_date;
                        tmp.utg_id = rec.utg_id;
                        tmp.rec_content = rec.rec_content;
                        tmp.rec_title = rec.rec_title;
                        tmp.rec_isdeleted = rec.rec_isdeleted;

                        db.SaveChanges();
                    }

                    rec = current as Recommendation;
                }
            }

            return rec;
        }
    }
}
